//
//  TGMapView.m
//  ConvenientlyTake
//
//  Created by 张奇 on 2023/1/11.
//

#import "TGMapView.h"
#import "CustomAnnotationView.h"

@interface TGMapView ()<MAMapViewDelegate>

@property (nonatomic, strong) MAMapView *mapView;

//@property (nonatomic, strong, readwrite) CustomCalloutView *calloutView;

@end

@implementation TGMapView

- (instancetype)initWithFrame:(CGRect)frame
                  coordinates:(CLLocationCoordinate2D *)coords
              annotationCount:(NSInteger)annotationCount
           endPointAnnotation:(MAPointAnnotation *)endPointAnnotation {
    if (self = [super initWithFrame:frame]) {
        
        [AMapServices sharedServices].enableHTTPS = YES;
        
        ///初始化地图
        self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.mapView.delegate = self;
        
        ///把地图添加至view
        [self addSubview:_mapView];
        
//        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
//        pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.902136, 116.44095);
//        pointAnnotation.title = @"08月09日签收";
//        pointAnnotation.subtitle = @"郑州市";
        
        [_mapView addAnnotation:endPointAnnotation];
        
        //构造折线数据对象
//        CLLocationCoordinate2D commonPolylineCoords[4];
//        commonPolylineCoords[0].latitude = 39.832136;
//        commonPolylineCoords[0].longitude = 116.34095;
//
//        commonPolylineCoords[1].latitude = 39.832136;
//        commonPolylineCoords[1].longitude = 116.42095;
//
//        commonPolylineCoords[2].latitude = 39.902136;
//        commonPolylineCoords[2].longitude = 116.42095;
//
//        commonPolylineCoords[3].latitude = 39.902136;
//        commonPolylineCoords[3].longitude = 116.44095;
        
        //构造折线对象
        MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:coords count:annotationCount];
        
        //在地图上添加折线对象
        [_mapView addOverlay:commonPolyline];
        //将折线显示在屏幕中心
        [TGGCD executeAfterTime:0.5 task:^{
            [self.mapView showOverlays:@[commonPolyline] animated:NO];
            self.mapView.zoomLevel = self.mapView.zoomLevel-0.5;
        }];
        
    }
    return self;
}

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        
        polylineRenderer.lineWidth    = 8.f;
        polylineRenderer.strokeColor  = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.6];
//        polylineRenderer.lineJoin = kCGLineJoinRound;
//        polylineRenderer.lineCap  = kCGLineCapRound;

        return polylineRenderer;
    }
    return nil;
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
//        annotationView.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
//        annotationView.animatesDrop = YES;        //设置标注动画显示，默认为NO
//        annotationView.draggable = YES;        //设置标注可以拖动，默认为NO
//        annotationView.pinColor = MAPinAnnotationColorPurple;
        
        annotationView.canShowCallout = NO;
        annotationView.image = kImage(@"error_single_check_icon");
//        annotationView.centerOffset = CGPointMake(0, 18);
        return annotationView;
    }
    return nil;
}

@end
